package com.offcn.scwproject.controller;


import com.offcn.dycommon.response.AppResponse;
import com.offcn.scwproject.pojo.*;
import com.offcn.scwproject.service.ProjectInfoServie;
import com.offcn.scwproject.vo.resp.ProjectDetailVo;
import com.offcn.scwproject.vo.resp.ProjectVo;
import com.offcn.utils.OssTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.management.resource.internal.ApproverGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "项目模块--包含图片的上传,查询所有的项目,项目的详情,项目的标签,项目的类型" )
@RequestMapping("/project")
@RestController
@Slf4j
public class ProjectInfoController {
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private ProjectInfoServie projectInfoServie;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AppResponse<Map<String,Object>> upload(@RequestParam("files") MultipartFile [] files) throws IOException {
        Map map = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        if(files != null && files.length >0){
            for (MultipartFile file : files) {
                if(!file.isEmpty()){
                    String url = ossTemplate.upload(file.getInputStream(), file.getOriginalFilename());
                    list.add(url);

                }
            }
        }
        map.put("urls",list);
        log.debug("urls的地址是:{}====={}集合是:",list,ossTemplate);
        return AppResponse.ok(map);

    }
    @ApiOperation("获取项目回报列表")
    @GetMapping("/details/returns/{projectId}")
    public AppResponse<List<TReturn>> getReturnList(@PathVariable("projectId") Integer projectId){
        List<TReturn> returnList = projectInfoServie.getReturnList(projectId);
        return AppResponse.ok(returnList);
    }

    //查询所有项目
    @ApiOperation("获取系统所有的项目")
    @GetMapping("/all")
    public AppResponse<List<ProjectVo>> findAllProject(){
        List<ProjectVo> list= new ArrayList<>();
        List<TProject> projectList = projectInfoServie.findProjectList();
        for (TProject tProject : projectList) {
            ProjectVo projectVo = new ProjectVo();
            BeanUtils.copyProperties(tProject,projectVo);
            List<TProjectImages> projectImageList = projectInfoServie.findProjectImageList(tProject.getId());
            for (TProjectImages tProjectImages : projectImageList) {
                if(tProjectImages.getImgtype() ==0){
                    projectVo.setHeaderImage(tProjectImages.getImgurl());
                }
            }
            list.add(projectVo);

        }
        return AppResponse.ok(list);
    }

    //查询项目详情
    @ApiOperation("获取项目信息详情")
    @GetMapping("/findProjectInfo/{projectId}")
    public AppResponse<ProjectDetailVo> findProjectInfo(@PathVariable("projectId") Integer projectId){
        //查询项目
        TProject projectByProjectId = projectInfoServie.findProjectByProjectId(projectId);
        ProjectDetailVo projectDetailVo = new ProjectDetailVo();
        //拷贝内容至vo
        BeanUtils.copyProperties(projectByProjectId,projectDetailVo);


        List<TProjectImages> projectImageList = projectInfoServie.findProjectImageList(projectId);
        ArrayList<String> strings =new ArrayList<>();//放详细图片
        for (TProjectImages tProjectImages : projectImageList) {
            //设置头图
            if(tProjectImages.getImgtype() ==0){
                projectDetailVo.setHeaderImage(tProjectImages.getImgurl());
            }else{
                strings.add(tProjectImages.getImgurl());
            }
        }
        //设置详情图片
        projectDetailVo.setDetailsImage(strings);
        //设置回报
        List<TReturn> returnList = projectInfoServie.getReturnList(projectId);
        projectDetailVo.setProjectReturns(returnList);

        return AppResponse.ok(projectDetailVo);


    }

    //所有的标签
    @ApiOperation("获取系统所有的项目标签")
    @GetMapping("/findAllTag")
    public AppResponse<List<TTag>> findAllTag(){
        return AppResponse.ok(projectInfoServie.findProjectTagList());
    }

    //所有的类型
    @ApiOperation("获取系统所有的项目分类")
    @GetMapping("/findAllType")
    public AppResponse<List<TType>> findAllType(){
        return AppResponse.ok(projectInfoServie.findProjectTypeList());
    }
    @ApiOperation("获取回报信息")
    @GetMapping("/returns/info/{returnId}")
    public AppResponse<TReturn> findReturn(@PathVariable("returnId") Integer returnId){
        TReturn tReturn = projectInfoServie. findProjectReturn(returnId);
        return AppResponse.ok(tReturn);
    }




}
